package com.taichuan.selfcheck.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.Target;
import com.taichuan.keysupport.Key;
import com.taichuan.selfcheck.R;
import com.taichuan.selfcheck.config.PropertyDefaultValue;
import com.taichuan.selfcheck.mvp.presenter.EmptyPresenter;
import com.taichuan.selfcheck.mvp.viewinterface.EmptyInterface;
import com.taichuan.selfcheck.ui.dialog.ResultDialog;
import com.taichuan.selfcheck.ui.fragment.base.AppBaseFragment;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 该类主要用于 实现 屏幕素质测试 - 彩条测试
 *
 * @author CamelLuo
 * @version 2021/7/1
 */
public class ScreenColorTestFragment extends AppBaseFragment<EmptyInterface, EmptyPresenter> implements EmptyInterface {
    private static final String TAG = ScreenColorTestFragment.class.getSimpleName();

    /**
     * 定义一个Handler事件用于 进入下一项测试项
     */
    private static final int MSG_STEP_NEXT = 1;

    @BindView(R.id.screen_color_test_content_layout)
    LinearLayout screenColorContentLayout;
    @BindView(R.id.screen_color_test_name_textView)
    TextView testNameTv;

    /**
     * 彩条测试中每一项测试的布局控件View 的集合
     */
    private List<LinearLayout> colorTestViewsList = new ArrayList<>();
    /**
     * 彩条测试中每一项测试的名称
     */
    private String[] colorTestNameArray;

    /**
     * 定义一个变量用于记录当前显示的测试项
     */
    private int stepCount = 0;

    ResultDialog resultDialog;
    private LinearLayout localLinearLayout2;


    @Override
    protected int setContentViewId() {
        return R.layout.fragment_screen_color_test;
    }

    @Override
    protected void initView(View view, Bundle bundle) {
        //设置弹窗发送的事件——屏幕颜色
        resultDialog = new ResultDialog(PropertyDefaultValue.FUNCTION_TEST_1,
                getResources().getString(R.string.btn_settings_screen_color_test), getActivity());

        //初始化 彩条测试所需的资源图片
        initColorTestResources();
        //展示第一个测试项
        nextStepColorTest(false,0);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //移除所有消息事件
        stepHandler.removeCallbacksAndMessages(null);
    }

    /**
     * 定义一个方法用于将彩条测试的各项测试图片 按照一定的显示比例 加载到布局文件中，并添加到List中
     * 以下为各测试项的名称，以及其对应的图片资源文件，及摆放位置和显示比例
     *                                          下(50%)       上(50%)
     * 彩条(VAG全范围)                         step01_left  step01_right
     *
     *                                          下(50%)       上(50%)
     * 彩条、灰阶(VGA全范围)                    step02_right  step02_left
     *
     *                                       左边(12.5%)    中间（75%）   右边(12.5%)
     * 彩条(ITU-R BT.601,100%)                黑边          step03_color  黑边
     *
     *                                       左边(12.5%)    中间（75%）   右边(12.5%)
     * 彩条(ITU-R BT.601,75%)                 黑边          step04_color  黑边
     *
     *                                         全屏（100%）
     * 彩条(ITU-R BT.709,100%)                step05_color
     *
     *                                         全屏（100%）
     * 彩条(ITU-R BT.709,75%)                 step06_color
     *
     *                                       下(weight=5)       中间(weight=1)       上(weight=3)
     * 彩条(SMPTE)                            step07_no3          step07_no2          step07_no1
     *
     *                                        最下(weight=90)   第二下(weight=13)   第二上(weight=13)   最上(weight=58)
     * 彩条(SMPTE HD)                         step08_no4        step08_no3          step08_no2          step08_no1
     */
    private void initColorTestResources() {
        //将彩条测试中的每一页测试的布局文件都添加到List中
        //彩条(VAG全范围)
        colorTestViewsList.add(add2ColorView(R.mipmap.step01_left, R.mipmap.step01_right));
        //彩条、灰阶(VGA全范围)
        colorTestViewsList.add(add2ColorView(R.mipmap.step02_left, R.mipmap.step02_right));
        //彩条(ITU-R BT.601,100%)
        colorTestViewsList.add(add1ColorView(R.mipmap.step03_color, true));
        //彩条(ITU-R BT.601,75%)
        colorTestViewsList.add(add1ColorView(R.mipmap.step04_color, true));
        //彩条(ITU-R BT.709,100%)
        colorTestViewsList.add(add1ColorView(R.mipmap.step05_color, false));
        //彩条(ITU-R BT.709,75%)
        colorTestViewsList.add(add1ColorView(R.mipmap.step06_color, false));
        //彩条(SMPTE)
        colorTestViewsList.add(add3ColorView(R.mipmap.step07_no1, R.mipmap.step07_no2, R.mipmap.step07_no3));
        //彩条(SMPTE HD)
        colorTestViewsList.add(add4ColorView(R.mipmap.step08_no1, R.mipmap.step08_no2, R.mipmap.step08_no3, R.mipmap.step08_no4));

        //加载彩条测试的每一项测试的测试名称
        colorTestNameArray = getResources().getStringArray(R.array.screen_color_test_item_value);
    }

    /**
     * 定义一个方法用于 添加 一个彩条图片资源到 布局控件中
     *
     * @param colorViewResId  彩条图片资源id
     * @param isNeedBlackSide 是否需要左右黑边
     * @return
     */
    private LinearLayout add1ColorView(int colorViewResId, boolean isNeedBlackSide) {
        LinearLayout localLinearLayout1 = new LinearLayout(getActivity());
        if (!isNeedBlackSide) {
            localLinearLayout1.setOrientation(LinearLayout.VERTICAL);
            localLinearLayout1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));
            localLinearLayout1.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

            ImageView imageView = new ImageView(getActivity());
            imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);

            localLinearLayout1.addView(imageView);

            Glide.with(getActivity()).load(colorViewResId).override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).into(imageView);
        } else {
            localLinearLayout1.setOrientation(LinearLayout.HORIZONTAL);
            localLinearLayout1.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

            //色条测试图片的宽度占屏幕宽度的0.75，两边黑边的宽度占屏幕宽度的0.125
            LinearLayout.LayoutParams colorLayoutParams = new LinearLayout.LayoutParams(0,
                    LinearLayout.LayoutParams.MATCH_PARENT, 0.75f);
            LinearLayout.LayoutParams blackSideLayoutParams = new LinearLayout.LayoutParams(0,
                    LinearLayout.LayoutParams.MATCH_PARENT, 0.125f);

            View localView1 = new View(getActivity());
            localView1.setLayoutParams(blackSideLayoutParams);

            View localView2 = new View(getActivity());
            localView2.setLayoutParams(blackSideLayoutParams);

            ImageView localLinearLayout2 = new ImageView(getActivity());
            localLinearLayout2.setScaleType(ImageView.ScaleType.FIT_XY);
            localLinearLayout2.setLayoutParams(colorLayoutParams);

            localLinearLayout1.addView(localView1);
            localLinearLayout1.addView(localLinearLayout2);
            localLinearLayout1.addView(localView2);

            localView1.setBackgroundColor(getResources().getColor(R.color.black));
            localView2.setBackgroundColor(getResources().getColor(R.color.black));

            Glide.with(getActivity()).load(colorViewResId).override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).into(localLinearLayout2);
        }
        return localLinearLayout1;
    }

    /**
     * 定义一个方法用于 添加 两个彩条图片资源到 布局控件中
     *
     * @param colorView1ResId 下方 彩条图片资源id
     * @param colorView2ResId 上方 彩条图片资源id
     * @return
     */
    private LinearLayout add2ColorView(int colorView1ResId, int colorView2ResId) {
        LinearLayout localLinearLayout = new LinearLayout(getActivity());
        localLinearLayout.setOrientation(LinearLayout.VERTICAL);
        localLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        localLinearLayout.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        ImageView localImageView1 = new ImageView(getActivity());
        localImageView1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT, 1.0F));
        localImageView1.setScaleType(ImageView.ScaleType.FIT_XY);

        ImageView localImageView2 = new ImageView(getActivity());
        localImageView2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT, 1.0F));
        localImageView2.setScaleType(ImageView.ScaleType.FIT_XY);

        localLinearLayout.addView(localImageView2);
        localLinearLayout.addView(localImageView1);

        Glide.with(getActivity()).load(colorView1ResId).override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).into(localImageView1);
        Glide.with(getActivity()).load(colorView2ResId).override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).into(localImageView2);

        return localLinearLayout;
    }

    /**
     * 定义一个方法用于 添加 三个彩条图片资源到 布局控件中
     *
     * @param colorView1ResId 下方 彩条图片资源id
     * @param colorView2ResId 中间 彩条图片资源id
     * @param colorView3ResId 上方 彩条图片资源id
     * @return
     */
    private LinearLayout add3ColorView(int colorView1ResId, int colorView2ResId, int colorView3ResId) {
        LinearLayout localLinearLayout1 = new LinearLayout(getActivity());

        localLinearLayout1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        localLinearLayout1.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        localLinearLayout1.setOrientation(LinearLayout.HORIZONTAL);

        //色条测试图片的宽度占屏幕宽度的0.75，两边黑边的宽度占屏幕宽度的0.125
        LinearLayout.LayoutParams colorLayoutParams = new LinearLayout.LayoutParams(0,
                LinearLayout.LayoutParams.MATCH_PARENT, 0.75f);
        LinearLayout.LayoutParams blackSideLayoutParams = new LinearLayout.LayoutParams(0,
                LinearLayout.LayoutParams.MATCH_PARENT, 0.125f);

        View blackSideView = new View(getActivity());
        View blackSideView2 = new View(getActivity());
        blackSideView.setLayoutParams(blackSideLayoutParams);
        blackSideView.setBackgroundColor(getResources().getColor(R.color.black));
        blackSideView2.setLayoutParams(blackSideLayoutParams);
        blackSideView2.setBackgroundColor(getResources().getColor(R.color.black));

        localLinearLayout2 = new LinearLayout(getActivity());
        localLinearLayout2.setLayoutParams(colorLayoutParams);
        localLinearLayout2.setOrientation(LinearLayout.VERTICAL);

        ImageView localImageView1 = new ImageView(getActivity());
        ImageView localImageView2 = new ImageView(getActivity());
        ImageView localImageView3 = new ImageView(getActivity());
        localImageView1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                0, 3.0F));
        localImageView1.setScaleType(ImageView.ScaleType.FIT_XY);

        localImageView2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                0, 1.0F));
        localImageView2.setScaleType(ImageView.ScaleType.FIT_XY);

        localImageView3.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                0, 5.0F));
        localImageView3.setScaleType(ImageView.ScaleType.FIT_XY);


        localLinearLayout2.addView(localImageView3);
        localLinearLayout2.addView(localImageView2);
        localLinearLayout2.addView(localImageView1);

        localLinearLayout1.addView(blackSideView);
        localLinearLayout1.addView(localLinearLayout2);
        localLinearLayout1.addView(blackSideView2);


        Glide.with(getActivity()).load(colorView1ResId).override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).into(localImageView1);
        Glide.with(getActivity()).load(colorView2ResId).override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).into(localImageView2);
        Glide.with(getActivity()).load(colorView3ResId).override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).into(localImageView3);

        return localLinearLayout1;
    }

    /**
     * 定义一个方法用于 添加 四个彩条图片资源到 布局控件中
     *
     * @param colorView1ResId 下方 彩条图片资源id
     * @param colorView2ResId 第二下 彩条图片资源id
     * @param colorView3ResId 第二上 彩条图片资源id
     * @param colorView4ResId 最上方 彩条图片资源id
     * @return
     */
    private LinearLayout add4ColorView(int colorView1ResId, int colorView2ResId, int colorView3ResId, int colorView4ResId) {
        LinearLayout localLinearLayout = new LinearLayout(getActivity());
        localLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        localLinearLayout.setOrientation(LinearLayout.VERTICAL);
        localLinearLayout.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        ImageView localImageView1 = new ImageView(getActivity());
        ImageView localImageView2 = new ImageView(getActivity());
        ImageView localImageView3 = new ImageView(getActivity());
        ImageView localImageView4 = new ImageView(getActivity());
        localImageView1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                0, 58.0F));
        localImageView1.setScaleType(ImageView.ScaleType.FIT_XY);
        localImageView2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                0, 13.0F));
        localImageView2.setScaleType(ImageView.ScaleType.FIT_XY);
        localImageView3.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                0, 13.0F));
        localImageView3.setScaleType(ImageView.ScaleType.FIT_XY);
        localImageView4.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                0, 90.0F));
        localImageView4.setScaleType(ImageView.ScaleType.FIT_XY);

        localLinearLayout.addView(localImageView4);
        localLinearLayout.addView(localImageView3);
        localLinearLayout.addView(localImageView2);
        localLinearLayout.addView(localImageView1);

        Glide.with(getActivity()).load(colorView1ResId).override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).into(localImageView1);
        Glide.with(getActivity()).load(colorView2ResId).override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).into(localImageView2);
        Glide.with(getActivity()).load(colorView3ResId).override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).into(localImageView3);
        Glide.with(getActivity()).load(colorView4ResId).override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).into(localImageView4);


        return localLinearLayout;
    }

    private final Handler stepHandler = new Handler(msg -> {
        switch (msg.what) {
            case MSG_STEP_NEXT: {
                boolean isStartByUser = (boolean) msg.obj;
                if (stepCount < colorTestViewsList.size()) {
                    //替换添加
                    screenColorContentLayout.removeAllViews();
                    screenColorContentLayout.addView(colorTestViewsList.get(stepCount));
                    //更新测试项名称
                    testNameTv.setText(colorTestNameArray[stepCount]);
                    //计数+1
                    stepCount++;
                    //发布定时任务，每5秒切换一个测试项
                    nextStepColorTest(false, 5 * 1000);
                } else {
                    //计数重置
                    stepCount = 0;
                    nextStepColorTest(false, 0);
                }
                break;
            }
        }
        return false;
    });

    @OnClick(R.id.screen_color_test_content_layout)
    @Override
    protected void onXmlClick(View v) {
        super.onXmlClick(v);
        switch (v.getId()) {
            case R.id.screen_color_test_content_layout: {
                nextStepColorTest(true, 0);
                break;
            }
        }
    }

    private void nextStepColorTest(boolean isStartByUser, int delayTime) {
        if (delayTime > 0) {
            stepHandler.sendMessageDelayed(stepHandler.obtainMessage(MSG_STEP_NEXT, isStartByUser), delayTime);
        } else {
            //移除延时任务
            stepHandler.removeMessages(MSG_STEP_NEXT);
            stepHandler.sendMessage(stepHandler.obtainMessage(MSG_STEP_NEXT, isStartByUser));
        }

    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (resultDialog.isShowing()) {
            resultDialog.dispatchKeyEvent(event);
        } else {
            if (event.getKeyCode() == Key.Convert.KEYCODE_JING) {
                screenColorContentLayout.performClick();
            }
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void getBundleValues(Bundle bundle) {

    }

    @Override
    protected boolean isAllowAutoFinishFragment() {
        return false;
    }

    @Override
    public void doOnAutoFinishFragment() {
        resultDialog.show();
    }

    @Override
    protected EmptyPresenter createPresenter() {
        return new EmptyPresenter();
    }
}
